﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Medianamik.Core
{
    [DebuggerDisplay("{Name}")]
    public class NodeTypeInterface : Entity<NodeTypeInterface>
    {
        public NodeTypeInterface(string name, string description)
        {
            _name = name;
            Description = description;
            Status = EntityStates.New;
        }

        public NodeTypeInterface(Guid id, string name, string description)
            : base(id)
        {
            _name = name;
            Description = description;
            Status = EntityStates.Loaded;
        }

        #region Properties

        private string _name;
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                Status = EntityStates.Dirty;
            }
        }

        public string LocalizedName
        {
            get
            {
                if (Status != EntityStates.New)
                {
                    return (string)System.Web.HttpContext.GetGlobalResourceObject("TypeManagement", ID + "_Label");
                }
                return string.Empty;
            }
        }

        public string Description { get; set; }

        public SubEntityCollection<NodeTypeInterface, NodeTypeInterfaceProperty> Properties
        {
            get
            {
                EnsureLoadProperties();
                return _properties;
            }
        }
        //public ReadOnlySubEntityCollection<NodeTypeInterface, NodeTypeInterfaceProperty> Properties
        //{
        //    get
        //    {
        //        return PropertiesInternal.AsReadOnly();
        //    }
        //}
        private SubEntityCollection<NodeTypeInterface, NodeTypeInterfaceProperty> _properties;

        public IEnumerable<NodeTypeInterfaceProperty> LookupProperties
        {
            get { return Properties.Where(prop => prop.PrimaryTypeID.HasValue); }
        }

        public bool HasProperty(string name)
        {
            return Properties.Any(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }

        #endregion

        #region IEquatable<NodeTypeInterface> Members

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        private List<IProperty> _eligibleProperties;
        public IEnumerable<IProperty> EligibleProperties
        {
            get
            {
                if (_eligibleProperties == null)
                    _eligibleProperties = NodeTypeManager.GetEligibleProperties(ID).ToList();

                return _eligibleProperties;
            }
        }

        #endregion

        #region Methods

        private void EnsureLoadProperties()
        {
            if (_properties == null)
            {
                _properties = new SubEntityCollection<NodeTypeInterface, NodeTypeInterfaceProperty>(this);
            }
            else { return; /* Les propriétés sont déjà chargées */ }

            _properties.AddRange(NodeTypeManager.GetNodeTypeInterfaceProperties(ID));
        }

        public bool PropertyNameExists(string name, out Guid propertyId)
        {
            NodeTypeInterfaceProperty prop = Properties
                .FirstOrDefault(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

            if (prop != null)
            {
                propertyId = prop.ID;
                return true;
            }

            propertyId = Guid.Empty;
            return false;
        }

        #endregion
    }
}
